/*
 * 
 * Created on Aug 27, 2005
 */
package com.wgo.precise.client.ui.view.util;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;

import com.wgo.precise.client.ui.controller.RequirementPlugin;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: ModelByteArrayTransfer.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public class ModelByteArrayTransfer extends ByteArrayTransfer {

    // First attempt to create a UUID for the type name to make sure that
    // different Eclipse applications use different "types" of
    // <code>LocalSelectionTransfer</code>
    private static final String TYPE_NAME = "local-selection-transfer-format" + (new Long(System.currentTimeMillis())).toString(); //$NON-NLS-1$;

    private static final int TYPEID = registerType(TYPE_NAME);

    private ISelection selection;

    private long selectionSetTime;

    private Map<Integer,Object> transfers = new HashMap<Integer,Object>();
    
    private Integer id = new Integer(0);
    
    private static ModelByteArrayTransfer instance = new ModelByteArrayTransfer();

    /**
     * Only the singleton instance of this class may be used. 
     */
    private ModelByteArrayTransfer() {
    }



    /**
     * Returns the singleton.
     * @return LocalSelectionTransfer
     */
    public static ModelByteArrayTransfer getInstance() {
        return instance;
    }
    
    /**
     * Returns the local transfer data.
     * 
     * @return the local transfer data
     */
    public ISelection getSelection() {
        return selection;
    }

    /**
     * Returns the type id used to identify this transfer.
     * 
     * @return the type id used to identify this transfer.
     */
    @Override
	protected int[] getTypeIds() {
        return new int[] { TYPEID };
    }

    /**
     * Returns the type name used to identify this transfer.
     * 
     * @return the type name used to identify this transfer.
     */
    @Override
	protected String[] getTypeNames() {
        return new String[] { TYPE_NAME };
    }

    /**
     * @see org.eclipse.swt.dnd.Transfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
     */
    @Override
    protected synchronized void javaToNative(Object object, TransferData transferData) {
    	byte[] bytes = new byte[]{id.byteValue()};
    	try {
    		super.javaToNative(bytes, transferData);
    	} catch (Exception e) {
    		System.out.println("Error: ");
    		e.printStackTrace();
    	}
    	transfers.put(id, object);
        id++;
    }

    /**
	 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
	 */
	@Override
	protected Object nativeToJava(TransferData transferData) {
		Object result = super.nativeToJava(transferData);
		if (result instanceof byte[]) {
			byte[] bytes = (byte[]) result;
			int i = (new Byte(bytes[0])).intValue();
			RequirementPlugin.log(getClass(), "nativeToJava id: " + i);
			result = transfers.remove(i);
		} else {
			RequirementPlugin.log(getClass(), "Not valid java-type: " + result.getClass().getName());
			result = null;
		}
		return result;
	}
    
	/**
     * Sets the transfer data for local use.
     * 
     * @param s the transfer data
     */
    public void setSelection(ISelection s) {
        selection = s;
    }

    /**
     * Returns the time when the selection operation
     * this transfer is associated with was started.
     * 
     * @return the time when the selection operation has started
     * 
     * @see org.eclipse.swt.events.TypedEvent#time
     */
    public long getSelectionSetTime() {
        return selectionSetTime;
    }

    /**
     * Sets the time when the selection operation this
     * transfer is associated with was started.
     * If assigning this from an SWT event, be sure to use
     * <code>setSelectionTime(event.time & 0xFFFF)</code>
     * 
     * @param time the time when the selection operation was started
     * 
     * @see org.eclipse.swt.events.TypedEvent#time
     */
    public void setSelectionSetTime(long time) {
        selectionSetTime = time;
    }

    /**
     * Removes cached java-transfer-object to avoid memory-leak.
     * Should be called from own implementation of org.eclipse.swt.dnd.DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
     * 
     * @param id
     */
    public static void clearTransfer(Integer id) {
        instance.transfers.remove(id);
    }
    
    public static Transfer[] getDefaultTrancfer() {
    	return new Transfer[] { instance };
    }
}
/**
 * 
 * Changelog:
 * 
 * $Log: ModelByteArrayTransfer.java,v $
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.2  2005-12-08 20:36:57  petterei
 * added gui to assign propertydefinitionsets to categories
 *
 * Revision 1.1  2005/10/11 15:08:03  petterei
 * Seccond iteration:
 * 1. Eclipse based client with new domain model
 * 2. Flexible Project and Category hierarchy with Drag-n-Drop functionality
 * New functionality:
 * 3. Eclipse plugin update-site
 *
 * Revision 1.8  2005/09/27 17:06:19  petterei
 * *** empty log message ***
 *
 * Revision 1.7  2005/09/26 17:47:22  petterei
 * release conflicts, step 2
 *
 * Revision 1.5  2005/09/01 11:05:09  petterei
 * cleaned up (removed unused code)
 *
 * Revision 1.4  2005/09/01 11:03:12  petterei
 * fixed dnd in user-defined-types-editor for windows
 *
 * Revision 1.3  2005/09/01 07:33:52  petterei
 * test
 *
 * Revision 1.2  2005/08/31 21:43:31  petterei
 * +partly fixed dnd
 *
 * Revision 1.1  2005/08/28 18:01:26  peide3
 * save in Eclipse-way
 * dynamic propertyspecifications bug fixed
 * dnd to deside order of property specification
 * ItemType to make convertion more understandable
 *
 */
